/*
 * @Author: your name
 * @Date: 2020-10-15 08:34:36
 * @LastEditTime: 2021-02-21 15:40:28
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \sende_nuxt\store\index.js
 */
// 获取编译启动的默认语言
import Locale from '~/locales'
import Cookies from 'js-cookie'

// 获取编译启动的默认语言
const ENV_LANGUAGE = process.env.LANGUAGE ? process.env.LANGUAGE : 'en';

const state = () => ({
  locales: Locale(),
  locale: ENV_LANGUAGE,
  isShow: false,
  userInfo: {
    name: 'a123',
  },
  token: '',
  cartNum: 0,
  favouritesNum: 0,
  tabsValue: '',
  menu: [],
  curRoute: [], // 存储当前路径层级
});

const mutations = {
  /** 保存全局默认语言 **/
  SET_LANGUAGE(state, locale) {
    if (state.locales.indexOf(locale) !== -1) {
      Cookies.set('language', locale);
      state.locale = locale
    }
  },
  SET_SHOW(state, value) {
    state.isShow = value
  },
  // 设置用户细心
  SET_USERINFO(state, value) {
    state.userInfo = value;
  },
  // 设置token
  SET_TOKEN(state, value) {
    state.token = value
    Cookies.set('token', token);
  },
  // 设置购物车数量
  SET_CARTNUM(state, value) {
    state.cartNum = value
  },
  // 设置喜好
  SET_FAVOURITESNUM(state, value) {
    state.favouritesNum = value
  },
  // 退出登录
  SET_LOGOUT(state) {
    state.token = '';
    Cookies.remove('token');
  },
  // 切换问答选项
  SET_QUESTION(state, value) {
    state.tabsValue = value
  },
  // 初始化导航菜单
  SET_NAV(state, value) {
    state.menu = value;
  },
  // 设置当前路由层级
  SET_ROUTE(state, value) {
    state.curRoute = value
  }
};
const getters = {
  cartNum: (state) => () => {
    return state.cartNum
  },
  favouritesNum: (state) => () => {
    return state.favouritesNum
  },
  isToken: (state) => () => {
    return state.isToken
  }
}
const actions = {

}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
